<template>
  <el-dialog
    :title="dialogTitle"
    :visible.sync="dialogVisible"
    width="500px"
    @close="handleClose"
  >
    <el-form :model="form" :rules="rules" ref="form" label-width="100px" size="small">
      <el-form-item label="分类名称" prop="name">
        <el-input v-model="form.name" placeholder="请输入分类名称" @keyup.enter.native="handleSubmit"/>
      </el-form-item>

      <el-form-item label="优先级" prop="rank">
        <el-input-number 
          v-model="form.rank" 
          :min="1"
          :max="999"
          placeholder="请输入优先级"
          :controls="false"
        ></el-input-number>
        <div class="form-tip">数字越小优先级越高</div>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="handleClose">取 消</el-button>
      <el-button type="primary" @click="handleSubmit" :loading="loading">确 定</el-button>
    </div>
  </el-dialog>
</template>

<script>
import api from '@/common/api'

export default {
  name: 'TagClassificationEditDialog',
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    classification: {
      type: Object,
      default: () => ({})
    }
  },
  data() {
    return {
      dialogVisible: false,
      loading: false,
      form: {
        id: undefined,
        name: '',
        rank: 100
      },
      rules: {
        name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
        ],
        rank: [
          { required: true, message: '请输入优先级', trigger: 'blur' },
          { type: 'number', message: '优先级必须为数字', trigger: 'blur' }
        ]
      }
    }
  },
  computed: {
    dialogTitle() {
      return this.form.id ? '编辑标签分类' : '添加标签分类'
    }
  },
  watch: {
    visible(val) {
      this.dialogVisible = val
      if (val) {
        this.initForm()
      }
    },
    dialogVisible(val) {
      if (!val) {
        this.$emit('update:visible', false)
      }
    }
  },
  methods: {
    initForm() {
      if (this.classification.id) {
        this.form = { ...this.classification }
      } else {
        this.form = {
          id: undefined,
          name: '',
          rank: 100
        }
      }
    },
    handleClose() {
      this.$refs.form?.resetFields()
      this.dialogVisible = false
    },
    handleSubmit() {
      this.$refs.form.validate(async valid => {
        if (!valid) return

        this.loading = true
        try {
          const method = this.form.id ? 'admin_updateTagClassification' : 'admin_addTagClassification'
          const { data } = await api[method](this.form)
          this.$message.success(this.form.id ? '更新成功' : '添加成功')
          this.$emit('success', data.data)
          this.handleClose()
        } catch (error) {
          this.$message.error(error.message || '操作失败')
        } finally {
          this.loading = false
        }
      })
    }
  }
}
</script>

<style scoped>
.form-tip {
  font-size: 12px;
  color: #909399;
  margin-top: 4px;
}

.el-input-number {
  width: 100%;
}
</style>
